package com.competition.web.admin.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.param.impl.IdPageParam;
import com.admin.vo.CompanyInfoVO;
import com.admin.vo.VisitCustomerVO;
import com.admin.vo.contract.ICompanyInfoVO;
import com.response.vo.ResponseModel;
import com.returns.value.ICodeAndData;
import com.unify.type.Department;
import com.unify.type.DepartmentFlatten;
import com.controller.base.AbstractCommonController;
import com.msg.ResponseCode;
import com.page.PageResponseData;
import com.competition.web.entity.Config;
import com.competition.web.entity.VisitCustomer;
import com.competition.param.impl.AnchorEnterpriseParam;
import com.competition.param.impl.DesignatedMegotiatorData;
import com.competition.param.impl.OrganizeRequestParam;
import com.competition.param.impl.QueryEligibleEnterprisesRequestParm;
import com.competition.param.impl.RedisTotal;
import com.competition.param.impl.VisitRequestParam;
import com.competition.web.admin.config.HttpRequest;
import com.competition.web.admin.service.IConfigService;
import com.competition.web.admin.service.IVisitCustomerService;
import com.basic.service.contract.IBaseService;

@RestController
@RequestMapping("visitCustomer")
public class VisitCustomerController extends AbstractCommonController<VisitCustomer, IdPageParam> {

	private IVisitCustomerService service;

	private IConfigService configService;
	
	private HttpRequest httpRequest;

	@Resource(name = "visitCustomerServiceImpl")
	public void setService(IVisitCustomerService service) {
		this.service = service;
	}
	
	/**
	 * 设置锚点企业信息
	 * @return
	 */
	@PostMapping("getAnchorEnterprise")
	public ResponseModel<Integer> getAnchorEnterprise(@RequestBody @Validated AnchorEnterpriseParam anchorEnterpriseParam, HttpServletRequest httpServletRequest) {
		
		ICodeAndData<ICompanyInfoVO> anchorEnterpriseCompanyInfoVO = this.service.getnnchorEnterpriseCompanyInfoVO(anchorEnterpriseParam, this.getToken(httpServletRequest));
		
		if (anchorEnterpriseCompanyInfoVO.getStatus() == ResponseCode.ERROR.getCode()) {
			return ResponseModel.errorMsg(0, anchorEnterpriseCompanyInfoVO.getMsg());
		}
		
		return ResponseModel.success(1);
	}
	
	
	@PostMapping("removeCache")
	public ResponseModel<Integer> removeCache(@RequestBody @Validated RedisTotal redisTotal , HttpServletRequest httpServletRequest) {
		
		this.service.removeCacheResult(this.getToken(httpServletRequest), redisTotal.getTotal());
		
		return ResponseModel.success(1);
		
	}
	
	
	/**
	 * 获取加盟部二道组织架构
	 * 指定谈判人
	 * @return
	 */
	@PostMapping("organize")
	public ResponseModel<DepartmentFlatten[][]> getOrganize(@RequestBody @Validated OrganizeRequestParam organizeRequestParam, HttpServletRequest httpServletRequest) {
		
		
		ICodeAndData<DepartmentFlatten[][]> organize = this.service.getDesignatedNegotiatorAndInvitationDepartment(
				organizeRequestParam, 
				this.getToken(httpServletRequest)
		);
		
		if (ResponseCode.ERROR.getCode() == organize.getStatus()) {
			return ResponseModel.errorMsg(null, organize.getMsg());
		}
		
		return ResponseModel.success(organize.getData());
	}
	
	/**
	 * 获取电邀部员工
	 * @return
	 */
	@PostMapping("invitationName")
	public ResponseModel<List<Department>> invitationName() {
		
		return ResponseModel.<List<Department>>success(this.service.getInvitationName());
	}
	
	/**
	 * 获取符合的公司列表的数量及页数
	 * @param enterprisesRequestParm
	 * @return
	 */
	@PostMapping("getCompanyList")
	public ResponseModel<PageResponseData<CompanyInfoVO>> getCompanyList(
			@RequestBody @Validated QueryEligibleEnterprisesRequestParm<DesignatedMegotiatorData> enterprisesRequestParm,
			HttpServletRequest httpServletRequest
	) {
		
		Config config = this.configService.getConfigFromCache();
		
		return ResponseModel.<PageResponseData<CompanyInfoVO>>success(
				this.service.companyListForSearch(
					enterprisesRequestParm, 
					CompanyInfoVO.class, 
					this.getToken(httpServletRequest),
					Integer.valueOf(config.getVal())
		));
		
	}
	
	/**
	 * 从redis获取符合的公司列表
	 * @return
	 */
	@PostMapping("getCompanyInfoVOs")
	public ResponseModel<List<ICompanyInfoVO>> getCompanyInfoVOs(HttpServletRequest httpServletRequest) {
		
		return ResponseModel.<List<ICompanyInfoVO>>success(this.service.getCompanyInfoVOs(this.getToken(httpServletRequest)));
		
	}
	
	
	
	

	/**
	 * 有 131723 这个key 代表 二访 99808 --> 首访核单状态 130418 ---> 回访核单状态 拉取探马数据
	 * 
	 * @param horses
	 * @param httpServletRequest
	 * @return
	 */
	@PostMapping("/exploringHorses")
	public ResponseModel<Object> exploringHorses(
			@RequestBody /* @Validated ExploringHorses */ VisitRequestParam[] horses,
			HttpServletRequest httpServletRequest) {


		List<Object> exploringHorsesResponses = new ArrayList<Object>();

		for (VisitRequestParam visitRequestParam : horses) {
			Object exploringHorsesResponse = httpRequest.postExploringHorsesSync(
					/* visitRequestParam */ visitRequestParam,
					new ParameterizedTypeReference<Object>() {
					});
			if (null == exploringHorsesResponse) {
//				return ResponseModel.success(exploringHorsesResponse);
				return ResponseModel.errorMsg(0, "请求探马失败！！！");
			}

//			if (0 != exploringHorsesResponse.getCode()) {
//				return ResponseModel.errorMsg(0, exploringHorsesResponse.getMessage());
//			}

			exploringHorsesResponses.add(exploringHorsesResponse);
		}

		return ResponseModel.success(exploringHorsesResponses);
	}

	


	private String getToken(HttpServletRequest httpServletRequest) {
		return httpServletRequest.getHeader("Authorization");
	}




	
	@SuppressWarnings("unchecked")
	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) VisitCustomerVO.class;
	}


	@Resource
	public void setHttpRequest(HttpRequest httpRequest) {
		this.httpRequest = httpRequest;
	}



	@Override
	protected IBaseService<VisitCustomer> getBaseService() {
		// TODO Auto-generated method stub
		return this.service;
	}

	@Resource
	public void setConfigService(IConfigService configService) {
		this.configService = configService;
	}
}